package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dao.SetmealDao;
import com.itheima.reggie.dao.SetmealDishDao;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Page;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealDishDao setmealDishDao;
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        setmealDto.setCode("000000");
        setmealDao.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setSort(Math.abs(new Random().nextInt()));
            setmealDishDao.save(setmealDish);
        });
    }

    @Override
    public Page<SetmealDto> findByPage(Integer pageNum, Integer pageSize, String name) {
        List<SetmealDto> setmealList = setmealDao.findPageByName(name);
        return new Page<>(setmealList);
    }

    @Override
    public void deleteById(Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealDao.findById(id);
            Integer status = setmeal.getStatus();
            if (status == 0 ){
                setmealDao.deleteById(id);
                setmealDishDao.deleteByid(id);
            }else {
                throw new CustomException("商品启售中，无法删除");
            }
        }
    }

    @Override
    public void updateStatus(Integer status, Long[] ids) {
        for (Long id : ids) {
            setmealDao.updateStatus(status, id);
        }
    }

    @Override
    public void updateSetMeal(SetmealDto setmealDto) {
        setmealDao.update(setmealDto);
        Long setMealId = setmealDto.getId();
        setmealDishDao.deleteByid(setMealId);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setMealId);
            setmealDish.setSort(0);
            setmealDishDao.save(setmealDish);
        });
    }

    @Override
    public SetmealDto findSetmeal(Long id) {
        Setmeal setmeal = setmealDao.findById(id);
        List<SetmealDish> setmealDishGl = setmealDishDao.findSetmealDishGl(id);
        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setSetmealDishes(setmealDishGl);
        BeanUtils.copyProperties(setmeal,setmealDto);
        return setmealDto;
    }


    @Override
    public List<Setmeal> findBysetmealId(Long categoryId, Integer status) {
        List<Setmeal> bysetmealId = setmealDao.findBysetmealId(categoryId, status);
        return bysetmealId;
    }

    @Override
    public List<Map<String, Object>> findSetmealDish(Long setmealId) {
        return setmealDishDao.findSetmealDish(setmealId);
    }
}
